package org.open.sharedRadar;

import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnCreateContextMenuListener;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;
import android.widget.AdapterView.OnItemLongClickListener;

public class ManageFriends extends ListActivity {
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.friends_menu, menu);
	    return true;
		//return super.onCreateOptionsMenu(menu);
	}
    
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    // Handle item selection
	    switch (item.getItemId()) {
	    case R.id.newRequest:
	    	AddFriend();
	        return true;	    
	    default:
	        return super.onOptionsItemSelected(item);
	    }
	}
	
	@Override
	public boolean onContextItemSelected(MenuItem item) {
       	if(item.getTitle()=="Confirma")
       	{
       		Confirma();
       	}
    	else if(item.getTitle()=="Ignora")
    	{
    		Ignora();
    	}
       	myDBAdapter.open();
       	values = myDBAdapter.getFriendsRequests(); 
       	myDBAdapter.close();
       	//adapter.notifyDataSetChanged();
       	adapter.getCursor().requery();
       	return true;
	}

    public void Confirma(){    	
		String result = handler.confirmIgnoreFriends(friendId, false);		
    	if(result.toUpperCase().equals("OK")) 
    	{
    		myDBAdapter.open();
    		myDBAdapter.updateDeleteFriend(friendId, true);
    		myDBAdapter.close();
    		Toast.makeText(ManageFriends.this, "Cererea a fost confirmata! Acum va puteti gasi reciproc pe harta", Toast.LENGTH_SHORT).show();
    	}
    }
    public void Ignora(){    	
		
    	String result = handler.confirmIgnoreFriends(friendId, true);		
    	if(result.toUpperCase().equals("OK")) 
    	{
    		myDBAdapter.open();
    		myDBAdapter.updateDeleteFriend(friendId, false);
    		myDBAdapter.close();
    		Toast.makeText(ManageFriends.this, "Prietenul a fost sters!", Toast.LENGTH_SHORT).show();
    	}
    }
    public void AddFriend()
    {
    	addFriendDialog = new AddFriend(this);
    	addFriendDialog.setCancelable(true);
    	addFriendDialog.setParentActivity(this);
    	addFriendDialog.setLista(this);
    	addFriendDialog.show();    	
    }
    
	DBAdapter myDBAdapter = new DBAdapter(this);
	String friendId;
	String currentStatus;
	WebServiceHandler handler;
	ListView list;
	SimpleCursorAdapter adapter;
	Cursor values;
	AddFriend addFriendDialog;
	
	
	public void onCreate(Bundle icicle) 
	{
		handler = new WebServiceHandler();
		setContentView(R.layout.friend_list);
				
		myDBAdapter.open();
		values = myDBAdapter.getFriendsRequests();
		//myDBAdapter.close();
		startManagingCursor(values);				
        String[] columns = new String[] { "userName", "status" };
        int[] to = new int[] { R.id.name_entry, R.id.status_entry };
 		adapter = new SimpleCursorAdapter(
				this,R.layout.friend_entry,values, columns, to);						
		
 		setListAdapter(adapter);
		super.onCreate(icicle);
		list = getListView();
		list.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {
			
			public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) 
			{				
				menu.setHeaderTitle("Confirma prieten");				
				menu.add(0, v.getId(), 0, "Ignora");
				if(currentStatus.toUpperCase().equals("NECONFIRMAT"))
					menu.add(0, v.getId(), 0, "Confirma");
			}		    
		    
		})	;	
		list.setOnItemLongClickListener(new OnItemLongClickListener()
		{

			public boolean onItemLongClick(AdapterView<?> parent, View view,
					int position, long id) 
			{
				Cursor c = (Cursor) parent.getAdapter().getItem(position); 								
				friendId = c.getString(1);
				currentStatus = c.getString(2);
				list.showContextMenu();		        	
				return true;
			}
		});			
	}		
}